package org.ciallo.simplemusicplayer.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListAdapter;
import androidx.recyclerview.widget.RecyclerView;
import org.ciallo.simplemusicplayer.R;
import org.ciallo.simplemusicplayer.data.model.FolderInfo;

import java.util.Locale;
import java.util.Objects;

/**
 * @author Jukomu
 * @Description: 显示音乐文件夹列表的 RecyclerView 适配器
 * @Package: org.ciallo.simplemusicplayer.adapter
 * @Project: SimpleMusicPlayer
 * @name: FolderAdapter
 * @Date: 2025/6/3-01:18
 * @Filename: FolderAdapter
 */
public class FolderAdapter extends ListAdapter<FolderInfo, FolderAdapter.FolderViewHolder> {

    private final OnFolderClickListener listener;

    public interface OnFolderClickListener {
        void onFolderClick(FolderInfo folderInfo);
    }

    public FolderAdapter(OnFolderClickListener listener) {
        super(DIFF_CALLBACK);
        this.listener = listener;
    }

    private static final DiffUtil.ItemCallback<FolderInfo> DIFF_CALLBACK = new DiffUtil.ItemCallback<FolderInfo>() {
        @Override
        public boolean areItemsTheSame(@NonNull FolderInfo oldItem, @NonNull FolderInfo newItem) {
            // 文件夹路径是唯一的
            return Objects.equals(oldItem.path, newItem.path);
        }

        @Override
        public boolean areContentsTheSame(@NonNull FolderInfo oldItem, @NonNull FolderInfo newItem) {
            return oldItem.equals(newItem); // FolderInfo 需要正确实现 equals
        }
    };

    @NonNull
    @Override
    public FolderViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_folder, parent, false);
        return new FolderViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull FolderViewHolder holder, int position) {
        FolderInfo folderInfo = getItem(position);
        if (folderInfo != null) {
            holder.bind(folderInfo, listener);
        }
    }

    static class FolderViewHolder extends RecyclerView.ViewHolder {
        ImageView ivFolderIcon;
        TextView tvFolderName;
        TextView tvSongCount;

        FolderViewHolder(View itemView) {
            super(itemView);
            ivFolderIcon = itemView.findViewById(R.id.imageView_item_folder_icon);
            tvFolderName = itemView.findViewById(R.id.textView_item_folder_name);
            tvSongCount = itemView.findViewById(R.id.textView_item_folder_song_count);
        }

        void bind(final FolderInfo folderInfo, final OnFolderClickListener listener) {
            tvFolderName.setText(folderInfo.folderName);
            tvSongCount.setText(String.format(Locale.getDefault(), "%d首歌曲", folderInfo.songCount));

            // 图标已在XML中设置，如果需要根据文件夹类型改变图标，可以在这里添加逻辑

            itemView.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onFolderClick(folderInfo);
                }
            });
        }
    }
}
